<?php

namespace App\Models;

use App\Enums\HelpCenterStatusEnum;
use App\Http\Controllers\Admin\HelpCategoryController;
use App\Traits\ModelEventTrait;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class HelpCenter extends BaseModel
{
    use ModelEventTrait;
    use SoftDeletes;

    const KEY = 'id';
    const TABLE = 'sp_help_center';

    protected $table = self::TABLE;
    protected $primaryKey = self::KEY;
    protected $connection = 'mysql';

    // public $timestamps = false;
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
    const DELETED_AT = 'delete_time';

    protected $guarded = [];

    protected $appends = [
        'status_name',
    ];

    protected $hidden = [
        self::DELETED_AT,
    ];

    protected $casts = [
        'create_time' => 'datetime:Y-m-d H:i:s',
        'update_time' => 'datetime:Y-m-d H:i:s',
    ];

    public function getStatusNameAttribute()
    {
        return HelpCenterStatusEnum::getNameByKey($this->status);
    }

    /**
     * 关联规格组
     * @return BelongsTo
     */
    public function category()
    {
        return $this->belongsTo(HelpCategory::class, 'category_id', HelpCategory::KEY)->select('name','id');
    }



    public static function findById($id)
    {
        return self::query()->with('category')->find($id);
    }

}
